package com.yunshang.shoppingmall.shopping.productApi.controller;

import com.yunshang.shoppingmall.common.IndexProductUtil;
import com.yunshang.shoppingmall.common.WeinXinUtil;
import com.yunshang.shoppingmall.entity.*;
import com.yunshang.shoppingmall.service.ConfigService;
import com.yunshang.shoppingmall.service.IndexProductService;
import com.yunshang.shoppingmall.service.PannerService;
import com.yunshang.shoppingmall.service.ProductADVService;
import com.yunshang.shoppingmall.service.User.UserRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 *
 */

@Controller
public class OpenIndexController {

    @Autowired
    PannerService pannerService;

    @Autowired
    ProductADVService productADVService;
    @Autowired
    IndexProductService indexProductService;
    @Autowired
    UserRankService userRankService;
    @Autowired
    ConfigService configService;

    /*域名直接打开首页*/
    @RequestMapping("/")
    public ModelAndView openIndex(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        ModelAndView modelAndView = new ModelAndView("shoppingApi/product/index");
        HttpSession session = request.getSession();

        if(session.getAttribute("subscribe")!=null)
        {
            map.put("subscribe", 1);
        }
        else
        {
            map.put("subscribe", null);
        }
        UserNo userNo = (UserNo) request.getSession().getAttribute("userNo");
        int userNoId=0;
        if(userNo!=null){
            userNoId=userNo.getId();
        }
        //获取登录用户提成比

        //panner图
//        int pannerActiveSign = 1;
//        List<Panner> pannerList = pannerService.selectPanners(pannerActiveSign);
//
//        //首页热卖广告图
//        String advSgin = "30";
//        List<ProductADV> advList = productADVService.selectProductADVBySign(advSgin);
//        map.put("advList1", advList.get(0));
//        map.put("advList2", advList.get(1));
//        map.put("advList3", advList.get(2));
//        map.put("advList4", advList.get(3));
//        map.put("pannerLists", pannerList);

//        int userNoId = 0;
//        int userRankId = 0;
//        UserRank userRank = null;
//        double commission = 0.00;
//        double minyhPrice = 0.00;//最低优惠价格
//        double fenXiangZhuan = 0.00;//分享赚
//        double newUserCommission = 0.00;//新用户提成比
//        double yhcommission = 0.00;
//        double ykcommission = 0.00;
//        List commissionarr = new ArrayList();
//        UserNo userNo = (UserNo) request.getSession().getAttribute("userNo");
//        if (userNo != null) {
//            userNoId = userNo.getId();
//            userRankId = userNo.getRank();
//            userRank = userRankService.selectByPrimaryKey(userRankId);
//            commission = userRank.getCommission();
//            System.out.println("open----->commission--"+commission);
//            //新用户的提成比
//            List<UserRank> userRankList = userRankService.selectUserRankList();
//            //获取新用户的提成比
//
//            for (int i = 0; i < userRankList.size(); i++) {
//
//                    commissionarr.add(userRankList.get(i).getCommission());
//
//
//            }
//            Collections.sort(commissionarr);
//
//            newUserCommission = (double) commissionarr.get(0);
//            if(commission<=newUserCommission){
//                newUserCommission=0.00;
//            }
//            System.out.println("--open----commission->" + commission + "--yk-" + newUserCommission);
//
//        } else {
//            //未登录
//            List<UserRank> userRankList = userRankService.selectUserRankList();
//            for (int i = 0; i < userRankList.size(); i++) {
//                commissionarr.add(userRankList.get(i).getCommission());
//            }
//            Collections.sort(commissionarr);
//
//            //分享赚
//            commission = (double) commissionarr.get(0);
//            newUserCommission = (double) commissionarr.get(0);
//            System.out.println("--yh->" + commission + "--yk-" + newUserCommission);
//        }
//
//        //加载零售专区数据
//        IndexProduct indexProduct = new IndexProduct();
//        indexProduct.setActiveSign(4);
//        indexProduct.setProductUpState("Y");
//        List<IndexProduct> indexProductList = indexProductService.selectProductListByActiveSignAndProductUpState(indexProduct);
//        map.put("indexProductList", indexProductList);
//        List<IndexProduct> indexProductList1 = new ArrayList<>();
//
//        //分享赚以及优惠后最低
//        IndexProduct indexProductData = null;
//        for (int i = 0; i < indexProductList.size(); i++) {
//            indexProductData = indexProductList.get(i);
//            //优惠后最低
//            minyhPrice = IndexProductUtil.getMinyhPrice(indexProductData, commission);
//            indexProductData.setMinyhPrice(minyhPrice);
//            fenXiangZhuan = IndexProductUtil.getFenXiangZhuan(indexProductData, commission, newUserCommission);
//            System.out.println("----->fxz--" + fenXiangZhuan);
//            indexProductData.setFenXiangZhuan(fenXiangZhuan);
//            indexProductList1.add(indexProductData);
//        }
//        Config config = configService.selectByPrimaryKey(1);
//        map.put("indexProductList", indexProductList1);
//        map.put("config", config);
//        map.put("userNo", userNo);
//        map.put("newUserCommission", newUserCommission);
//        map.put("userCommission", commission);

        //加载零售专区数据
        modelAndView.addObject("map", map);
        modelAndView.addObject("userNoId",userNoId);
        return modelAndView;
    }

}
